package ink.xiaobaibai.controller.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.entity.Coupon;
import ink.xiaobaibai.entity.CouponUser;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.ICouponService;
import ink.xiaobaibai.service.ICouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 优惠券接口
 * @author: 小白白
 * @create: 2021-06-05
 **/

@RestController
@Api(tags = "优惠券接口")
@RequestMapping("/coupon")
@CrossOrigin
@ResourcesRoleMapping(resourceName = "优惠券操作", includeRoleNames = {MyRole.ADMIN, MyRole.POWER_COUPON})
@InsidePublicResources
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @Autowired
    private ICouponUserService couponUserService;

    @GetMapping("/list")
    @ApiOperation("获取当前优惠券列表")
    public ResponseFormat getCouponList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        QueryWrapper<Coupon> q1 = new QueryWrapper<>();
        q1.orderByDesc("create_time");
        Page<Coupon> page = this.couponService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    @GetMapping("/item/{id}")
    @ApiOperation("获取一个优惠券详细")
    public ResponseFormat getCouponItem(@PathVariable("id") Integer couponId) {
        return ResponseFormat.success(this.couponService.getById(couponId));
    }

    @GetMapping("/item/userList/{id}")
    @ApiOperation("获取优惠券的用户领取详细")
    public ResponseFormat getCouponUserList(@PathVariable("id") Integer couponId,
                                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        QueryWrapper<CouponUser> q1 = new QueryWrapper<>();
        q1.eq("coupon_id", couponId);
        Page<CouponUser> page = this.couponUserService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    /**
     * 增加优惠券
     */
    @PostMapping("/add")
    @ApiOperation("增加优惠券")
    public ResponseFormat addCoupon(@RequestBody @Validated Coupon coupon) {
        coupon.setId(null);
        coupon.setRemainingCount(coupon.getAllCount());
        coupon.setUseCount(0);
        this.couponService.save(coupon);
        return ResponseFormat.success();
    }

    /**
     * 修改优惠券
     */
    @PutMapping("/update")
    @ApiOperation("修改优惠券")
    public ResponseFormat updateCoupon(@RequestBody @Validated Coupon coupon) {
        //使用数量不可修改
        coupon.setUseCount(null);
        //剩余量不可修改 (下面自计算)
        coupon.setRemainingCount(null);
        Coupon oldCoupon = this.couponService.getById(coupon);
        if (oldCoupon == null) {
            return ResponseFormat.fail("请输入正确的优惠券id");
        }
        if (!oldCoupon.getType().equals(coupon.getType())) {
            return ResponseFormat.fail("业务类型不可修改");
        }
        if (oldCoupon.getAllCount() < coupon.getAllCount()) {
            //要增加剩余优惠券数量
            coupon.setRemainingCount(oldCoupon.getRemainingCount() + (coupon.getAllCount() - oldCoupon.getAllCount()));
        } else {
            coupon.setRemainingCount(oldCoupon.getRemainingCount());
        }
        if (oldCoupon.getAllCount() <= 0) {
            return ResponseFormat.fail("请输入正确的优惠券数量");
        }
        //总发行量改的太小了
        if (coupon.getRemainingCount() > coupon.getAllCount()) {
            coupon.setRemainingCount(coupon.getAllCount());
        }
        this.couponService.updateById(coupon);
        return ResponseFormat.success();
    }

    /**
     * 停用或启动优惠券
     */
    @PutMapping("/ss")
    @ApiOperation("停用或启用优惠券")
    public ResponseFormat startOrStopCoupon(@RequestParam("couponId") Integer couponId) {
        QueryWrapper<Coupon> q1 = new QueryWrapper<>();
        q1.select("id", "stop_use");
        q1.eq("id", couponId);
        Coupon coupon = this.couponService.getOne(q1);
        if (coupon == null) {
            return ResponseFormat.fail("请输入正确的优惠券id");
        }
        coupon.setStopUse(!coupon.getStopUse());
        this.couponService.updateById(coupon);
        return ResponseFormat.success();
    }

    @PutMapping("/pp")
    @ApiOperation("需要或者不需要授权手机号码后才能验证领取")
    public ResponseFormat startOrStopPhone(@RequestParam("couponId") Integer couponId) {
        QueryWrapper<Coupon> q1 = new QueryWrapper<>();
        q1.select("id", "phone_flag");
        q1.eq("id", couponId);
        Coupon coupon = this.couponService.getOne(q1);
        if (coupon == null) {
            return ResponseFormat.fail("请输入正确的优惠券id");
        }
        coupon.setPhoneFlag(!coupon.getPhoneFlag());
        this.couponService.updateById(coupon);
        return ResponseFormat.success();
    }

}
